package cc.lucien.controller;

import cc.lucien.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/web")
@SessionAttributes(value = {"user"})
public class DataController {

    @RequestMapping("/testData1")
    public String testData1(HttpServletRequest request){
        User user = new User();
        user.setId(33);
        user.setName("aliang");
        user.setUsername("lucien");
        user.setTel("12385247895");
        request.setAttribute("user",user);
        return "data";
    }

    @RequestMapping("/testData2")
    public String testData2(Map<String, Object> map) {
        User user = new User();
        user.setId(33);
        user.setName("aliang");
        user.setUsername("lucien");
        user.setTel("12385247895");
        map.put("user", user);
        return "data";
    }

    @RequestMapping("/getSessionAtt")
    public String getSessionAtt(@SessionAttribute User user) {
        System.out.println(user);
        return "hello";
    }

    // 不走视图解析图，走自己写的路径
    @RequestMapping("/myViewPath")
    public String myViewPath(Map<String, Object> map){
        User user = new User();
        user.setId(33);
        user.setName("aliang");
        user.setUsername("lucien");
        user.setTel("12385247895");
        map.put("user", user);

        // 走请求转发
        //return "forward:/WEB-INF/jsp/data.jsp";

        // 走重定向
        return "redirect:/static.html";
    }
}
